

import React, { useEffect } from 'react';
import { Suspense } from 'react';
import { memo } from 'react';
import { useDispatch } from 'react-redux';
import { renderRoutes } from 'react-router-config';
import { getLoginProfile } from '../../components/theme-login/store/actionCreator';
import routes from '../../router'
import { defaultInfo, setLoginInfo, getLoginInfo } from '@/utils/secret-key'
import { getCurSongIdx } from '../../utils/storage';
import { getSongDetailArrAction } from '../player/store/actionCreator'

const LOGIN_INFO = 'login_info'
const Wrapper = () => {
  const dispatch = useDispatch();

  useEffect(() => {
    if (localStorage.getItem(LOGIN_INFO)) {
      const { username, password } = getLoginInfo(LOGIN_INFO);
      username && password && dispatch(getLoginProfile(username, password))
    } else {
      setLoginInfo(LOGIN_INFO, defaultInfo)
    }
  }, [dispatch])
  useEffect(() => {
    const i = getCurSongIdx();
    dispatch(getSongDetailArrAction(i))
  }, [dispatch])
  return <>
    <Suspense fallback={null}>
      {
        renderRoutes(routes)
      }
    </Suspense>
  </ >
}
export default memo(Wrapper)